#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zip.h>

#define printf(fmt, ...) printf("%s %s %d dddddddddddd "fmt, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
int main(int argc, char **argv)
{
    char buffer[1024];
    char *path = argv[1];
    char *fname = NULL;
    int err = 0;
    struct zip *archive = NULL;
    struct zip_file *file = NULL;
    int n = 0;
    int i;

    archive = zip_open(path, 0, &err); if(archive == NULL) { char e[2048] = ""; zip_error_to_str(e, sizeof(e), err, 0); printf("open failed, err=%d[%s]\n", err, e); return -1; }
    //if (zip_set_default_password(archive, "123")) { printf("zip_set_default_password error\n"); }
    n = zip_get_num_files(archive); if(n < 0) { printf("get num of  failed\n"); zip_close(archive); return -1; }
    printf("num=%d\n", n);

    for(i=0; i<n; i++) {
        fname = (char *)zip_get_name(archive, i, 0); printf("file name: %s\n", fname);
        //file = zip_fopen_index(archive, i, ZIP_FL_ENCRYPTED);
        file = zip_fopen_index(archive, i, 0);
        if(file == NULL) {
            int z1 = 0, z2 = 0; zip_error_get(archive, &z1, &z2); char e[2048] = ""; zip_error_to_str(e, sizeof(e), z1, z2); printf("zip_fopen_index failed: %s\n", e);
            zip_close(archive);
            return -1;
        }

        memset(buffer, 0x00, sizeof(buffer));
        int l = zip_fread(file, buffer, 1023);
        printf("%.*s\n", l, buffer);
        zip_fclose(file);
    }

    zip_close(archive);
    return 0;
}
